import { defineStore } from 'pinia'

interface Tab {
  label: string
  value: string
}

export const useTabStore = defineStore('tab', {
  state: () => ({
    tabs: [{ label: 'menu.home', value: '/home' }] as Tab[],
  }),
  actions: {
    add(tab: Tab) {
      if (!tab.label) return
      if (!this.tabs.some((t) => t.value === tab.value)) {
        this.tabs.push(tab)
      }
    },
    remove(value: string) {
      this.tabs = this.tabs.filter((tab) => tab.value !== value)
    },
    init() {
      const tabs = localStorage.getItem('tabs')
      if (tabs) {
        this.tabs = JSON.parse(tabs) as Tab[]
      }
    },
    reset() {
      this.tabs = [{ label: 'menu.home', value: '/home' }]
    },
    storage() {
      const tabs = JSON.stringify(this.tabs)
      localStorage.setItem('tabs', tabs)
    },
  },
})
